package com.ruiyi.baseparser;

import java.io.IOException;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.ruiyi.error.RuiyiCredentialsException;
import com.ruiyi.error.RuiyiException;
import com.ruiyi.error.RuiyiParseException;

//import ruiyi.util.LogUtil;


public class ParserUtil {
	// private final static String TAG = "Util\t\t";

	public static RuiyiType consume(Parser<? extends RuiyiType> parser,
			String content) throws RuiyiCredentialsException,
			RuiyiParseException, RuiyiException, IOException {
		try {
			if (content.charAt(0) == '[') {
				JSONArray array = new JSONArray(content);
				return parser.parse(array);
			} else {
				JSONObject obj = new JSONObject(content);
				return parser.parse(obj);
			}
		} catch (JSONException ex) {
			throw new RuiyiException("Error parsing JSON response: "
					+ ex.getMessage());
		}
	}
}